package cn.xenosp.server.web.controller;

import cn.xenosp.server.common.Result;
import cn.xenosp.server.common.annotation.LogInfo;
import cn.xenosp.server.common.tools.ObjectConvert;
import cn.xenosp.server.module.activiti.entity.ProcessInstanceVO;
import cn.xenosp.server.module.activiti.entity.ProcessDefinitionVO;
import cn.xenosp.server.module.activiti.entity.SuspendDefinition;
import cn.xenosp.server.module.activiti.service.ProcessDefinitionService;
import cn.xenosp.server.module.activiti.service.ProcessInstanceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * 项目名： service-web
 * 包路径： cn.xenosp.server.web.controller
 * 作者：   TongHui
 * 创建时间: 2024-12-18 16:44
 * 部署流程
 * 1、设计器设计流程xml/png
 * 2、部署流程
 * 3、发起流程
 * 4、执行流程
 * 版本: 1.0
 */
@RestController
@RequestMapping("${app.config.api-base-url}/activiti/")
public class ProcessDefinitionController {
    @Autowired
    private ProcessDefinitionService processDefinitionService;
    @Autowired
    private ProcessInstanceService processInstanceService;

    @LogInfo(operationModuleType = "工作流定义管理",operationName = "查询列表",operationContent = "查询列表")
    @PostMapping("/getPage")
    public Result<?> getPage(@RequestBody String body){
        try {
            ProcessDefinitionVO processDefinitionVO = ObjectConvert.urlStringToClass(body,ProcessDefinitionVO.class);
            return Result.OK("成功",processDefinitionService.getByPaged(processDefinitionVO));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Result.error("失败",null);
    }


    @LogInfo(operationModuleType = "工作流定义管理",operationName = "部署流程",operationContent = "部署流程")
    @PostMapping("/deploy")
    public Result<?> deploy(@RequestBody ProcessDefinitionVO processDefinitionVO){
        try {
            processDefinitionService.deploy(processDefinitionVO);
            return Result.OK("成功");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Result.error("部署失败",null);
    }

    @LogInfo(operationModuleType = "工作流定义管理",operationName = "部署流程",operationContent = "部署流程")
    @PostMapping("/deployFile")
    public Result<?> deployFile(@RequestPart("file") MultipartFile file, @RequestBody ProcessDefinitionVO processDefinitionVO){
        try {
            processDefinitionService.deployFile(file,processDefinitionVO);
            return Result.OK("成功");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Result.error("部署失败",null);
    }

    @LogInfo(operationModuleType = "工作流定义管理",operationName = "查看流程文件",operationContent = "查看流程文件")
    @PostMapping("/edit")
    public Result<?> show(@RequestBody ProcessDefinitionVO processDefinitionVO){
        try {
            return Result.OK("成功",processDefinitionService.show(processDefinitionVO.getDeploymentId(),processDefinitionVO.getResourceName()));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Result.error("失败",null);
    }

    @LogInfo(operationModuleType = "工作流定义管理",operationName = "删除流程部署",operationContent = "删除流程部署")
    @PostMapping("/deleteDeploymentById")
    public Result<?> deleteDeploymentById(@RequestBody ProcessDefinitionVO processDefinitionVO){
        try {
            String msg = processDefinitionService.deleteById(processDefinitionVO.getId(),processDefinitionVO.getDeploymentId());
            return Result.OK(msg);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Result.error("删除失败",null);
    }

    @LogInfo(operationModuleType = "工作流定义管理",operationName = "流程信息校验",operationContent = "流程信息校验")
    @PostMapping("/verify")
    public Result<?> verify(@RequestBody ProcessDefinitionVO processDefinitionVO){
        try {
            return Result.OK("成功",processDefinitionService.verify(processDefinitionVO));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Result.error("失败",null);
    }

    @LogInfo(operationModuleType = "工作流定义管理",operationName = "将流程定义转为模型",operationContent = "将流程定义转为模型")
    @PostMapping("/exchange")
    public Result<?> exchange(@RequestBody ProcessDefinitionVO processDefinitionVO){
        try {
            return Result.OK("成功",processDefinitionService.exchangeToModel(processDefinitionVO.getId(), processDefinitionVO.getDesignType()));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Result.error("失败",null);
    }
    @LogInfo(operationModuleType = "工作流定义管理",operationName = "挂起流程定义",operationContent = "挂起流程定义")
    @PostMapping("/suspend")
    public Result<?> suspend(@RequestBody SuspendDefinition suspendDefinition){
        try {
            return Result.OK("成功",processDefinitionService.suspend(suspendDefinition));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Result.error("失败",null);
    }
    @LogInfo(operationModuleType = "工作流定义管理",operationName = "激活流程定义",operationContent = "激活流程定义")
    @PostMapping("/activate")
    public Result<?> activate(@RequestBody SuspendDefinition suspendDefinition){
        try {
            return Result.OK("成功",processDefinitionService.activate(suspendDefinition));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Result.error("失败",null);
    }

    @LogInfo(operationModuleType = "工作流管理",operationName = "流程实例",operationContent = "流程实例")
    @PostMapping("/processInstance")
    public Result<?> processInstance(@RequestBody String body){
        try {
            ProcessInstanceVO processInstanceVO = ObjectConvert.urlStringToClass(body, ProcessInstanceVO.class);
            return Result.OK("成功",processInstanceService.getByPaged(processInstanceVO));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Result.error("失败",null);
    }

    @LogInfo(operationModuleType = "工作流管理",operationName = "发起流程",operationContent = "发起流程")
    @PostMapping("/startProcess")
    public Result<?> startProcess(@RequestBody ProcessDefinitionVO processDefinitionVO){
        try {
            processDefinitionService.startProcess(processDefinitionVO.getId());
            return Result.OK("成功");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Result.error("发起失败");
    }

    @LogInfo(operationModuleType = "工作流管理",operationName = "完成流程",operationContent = "完成流程")
    @PostMapping("/completeTask")
    public Result<?> completeTask(@RequestBody ProcessInstanceVO processInstanceVO){
        try {
            processInstanceService.completeTask(processInstanceVO.getProcessInstanceId());
            return Result.OK("成功");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Result.error("发起失败");
    }
}
